import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import Home from '../views/controlHome/index.vue'
import controlUser from '../views/controlUser/index.vue'
import controlBlog from '../views/controlBlog/index.vue'
import Login from '../views/Login/index.vue'
import controlRole from '../views/controlRole/index.vue'
import createBlog from '../views/createBlog/index.vue'
import updateBlog from '../views/updateBlog/index.vue'
import userBlog from '../views/userBlog/index.vue'
import controlPermission from '../views/controlPermission/index.vue'
import baseLayout from '../layouts/base/index.vue'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'base',
    component: baseLayout,
    redirect:'/controlHome',
    children: [
      {
        path: '/controlHome',
        name: 'controlHome',
        component: Home,
        meta:{
          keepAlive:true
        }
      },
      {
        path: '/controlUser',
        name: 'controlUser',
        component: controlUser,
        meta:{
          keepAlive:true
        }
      },
      {
        path: '/controlRole',
        name: 'controlRole',
        component: controlRole,
        meta:{
          keepAlive:true
        }
      },
      {
        path: '/controlBlog',
        name: 'controlBlog',
        component: controlBlog,
        meta:{
          keepAlive:true
        }
      },
      {
        path: '/controlPermission',
        name: 'controlPermission',
        component: controlPermission,
        meta:{
          keepAlive:true
        }
      },
      {
        path: '/userBlog/:bId',
        name: 'userBlog',
        component: userBlog,
        meta:{
          keepAlive:true
        }
      },
    ],
  },
  {
    path: '/Login',
    name: 'Login',
    component: Login,
  },
  {
    path: '/createBlog',
    name: 'creatBlog',
    component: createBlog,
  },
  {
    path: '/updateBlog/:bId',
    name: 'updateBlog',
    component: updateBlog,
  },
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
})
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token');
  if (token || to.path === '/Login') {
    next();
  } else {
    next('/Login')
  }
})
export default router
